/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.propagation.events;

import ore.SpacecraftState;
import ore.errors.OrekitException;
import ore.frames.TopocentricFrame;

/** 
 * Detector of satellite raising/setting events.
 * 
 * This class finds elevation events (i.e. satellite raising
 * and setting).
 * 
 * The default implementation behavior is to continue propagation at
 * raising and to stop propagation at setting. This can be changed by
 * overriding the eventOccurred method in a derived class.
 * 
 * @author Luc Maisonobe
 */
public class ElevationDetector
    extends AbstractDetector
{

    /** 
     * Threshold elevation value.
     */
    private final double elevation;
    /** 
     * Topocentric frame in which elevation should be evaluated.
     */
    private final TopocentricFrame topo;


    /** 
     * The maximal interval between elevation checks should be smaller
     * than the half duration of the minimal pass to handle, otherwise
     * some short passes could be missed.
     * 
     * @param maxCheck Maximal interval in seconds
     * @param elevation Threshold elevation value
     * @param topo Topocentric frame in which elevation should be
     * evaluated
     */
    public ElevationDetector(double maxCheck, double elevation,
                             TopocentricFrame topo) {
        super(maxCheck, 1.0e-3);
        this.elevation = elevation;
        this.topo = topo;
    }


    /** 
     * @return Threshold elevation value
     */
    public double getElevation() {
        return this.elevation;
    }
    /** 
     * @return Topocentric frame
     */
    public TopocentricFrame getTopocentricFrame() {
        return this.topo;
    }
    /** 
     * Handle an elevation event and choose what to do next.
     * 
     * The default implementation behavior is to continue propagation
     * at raising and to stop propagation at setting. This can be
     * changed by overriding this method in a derived class.
     * 
     * @param s Current state information : date, kinematics, attitude
     * @param increasing If true, the value of the switching function increases
     * when times increases around event (note that increase is measured with respect
     * to physical time, not with respect to propagation which may go backward in time)
     * 
     * @return One of {@link #STOP}, {@link #RESET_STATE}, {@link #RESET_DERIVATIVES}
     * or {@link #CONTINUE}
     * 
     * @exception OrekitException Some specific error 
     */
    public int eventOccurred(SpacecraftState s, boolean increasing)
        throws OrekitException
    {
        return increasing ? CONTINUE : STOP;
    }
    /**
     * {@inheritDoc}
     */
    public double g(SpacecraftState s) throws OrekitException {
        return (topo.getElevation(s.getPositionVelocity().getPosition(), s.getFrame(), s.getDate()) - this.elevation);
    }

}
